package com.ynet.middleground.mobile.approve.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Throwables;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.commom.ResponseResultCount;
import com.ynet.middleground.mobile.approve.constant.ErrorMessageConstant;
import com.ynet.middleground.mobile.approve.request.QueryTaskPagerReq;
import com.ynet.middleground.mobile.approve.response.TaskListDTO;
import com.ynet.middleground.mobile.approve.service.ApproveCenterService;
import com.ynet.middleground.mobile.approve.service.UserCenterService;
import com.ynet.middleground.user.dto.ChannelDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName HomePageController
 * @Description 移动审批首页调用API Controller
 * @Author liwq
 * @Date 2021/3/19 14:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/")
@Api(value = "首页展示相关服务", tags = "首页展示相关服务")
public class HomePageController {

    @Autowired
    ApproveCenterService approveCenterService;

    @Autowired
    private UserCenterService userCenterService;

    /**
     * 分页查询审核任务列表
     * 
     * @param req
     * @return 任务列表
     */
    @PostMapping("task/list")
    @ApiOperation(value = "分页查询审核任务列表", notes = "分页查询审核任务列表")
    public ResponseResultCount<List<TaskListDTO>> listTaskByPage(@RequestBody @Valid QueryTaskPagerReq req) {
        ResponseResultCount<List<TaskListDTO>> serviceResult = new ResponseResultCount<>();
        try {
            List<TaskListDTO> taskListDTOS = approveCenterService.listTaskByPage(req);
            serviceResult.setData(taskListDTOS);
            serviceResult.setPageInfo(req.getPageInfo());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), " 分页查询任务列表服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0001_CODE, ErrorMessageConstant.ECUC0001_MESSAGE);
        }
        return serviceResult;
    }

    /**
     * 用户中台调用示例-查询渠道列表
     *
     * @return
     */
    @PostMapping("/channel/list")
    @ApiOperation(value = "查询渠道列表", notes = "查询渠道列表")
    public ResponseResult<List<ChannelDTO>> invokeUserChannel() {
        ResponseResult<List<ChannelDTO>> result = new ResponseResult<>();
        result.setData(userCenterService.channelList());
        return result;
    }

}
